package com.jdkreader.concurrency.concurrent.priority;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class PrioritizedTask implements Runnable,Comparable<PrioritizedTask>{

    private static int counter = 0;

    private Random random = new Random(47);

    private final int id = counter++;
    //任务优先级
    private final int priority;


    protected static List<PrioritizedTask> sequeue =
            new ArrayList<PrioritizedTask>();


    public PrioritizedTask(int priority) {
        this.priority = priority;
        sequeue.add(this);
    }

    @Override
    public int compareTo(PrioritizedTask task) {
        return Integer.compare(priority,task.priority);
//        return priority > task.priority ? 1 :
//                (priority < task.priority ? -1 : 0);
    }

    @Override
    public void run() {
        try{
            TimeUnit.MICROSECONDS.sleep(random.nextInt(250));
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public String toString(){
        return String.format("[%1$-3d]",priority)+" Task "+ id;
    }

    public String summary(){
        return "("+id+":"+priority+")";
    }

//    public static void main(String[] args) {
//
//        Random testRandomA = new Random(47);
//        Random testRandomB = new Random(47);
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//        System.out.println(Integer.compare(testRandomA.nextInt(250),testRandomB.nextInt(250)));
//    }
}
